﻿using QQZiFramework;
using System;
using System.Collections.Generic;

using System.Text;


namespace QQZiFramework.Membership
{
    /// <summary>
    /// membership接口定义
    /// </summary>
    public interface IMembership
    {
        DBContext CreateDBContext();
        string MessageAsyncPath { get; }

        /// <summary>
        /// 是否开发模式
        /// </summary>
        bool IsDevelopment();

        /// <summary>
        /// 用户类申明，比如圈子中就是User类
        /// </summary>
        Type UserType { get; }

        /// <summary>
        /// 读取获取设置Redis缓存Key
        /// </summary>
        /// <returns>处理后的key</returns>
        string OnRedisKey(string key);

        /// <summary>
        /// 读取获取设置系统缓存Key
        /// </summary>
        /// <returns>处理后的key</returns>
        string OnCacheKey(string key);

        /// <summary>
        /// 用户状态保存类型（cookie、session等）
        /// </summary>
        LoginUserStateType UserStateType { get; }

        /// <summary>
        /// 如何转cookie
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        String UserToCookie(BaseUser u);

        /// <summary>
        /// 如何取单条用户信息
        /// </summary>
        /// <param name="userid"></param>
        BaseUser GetRedisUser(int userId, DBContext context);

        /// <summary>
        /// 从Cookie中获取用户信息
        /// </summary> 
        BaseUser GetCookieUser(string cookieValue);

        /// <summary>
        /// 设置为登录状态,单独调该方法不会设置DBContext.LoginTokenInfo,如需要设置请使用DBContext.SetUserLoginState方法
        /// </summary>
        /// <param name="userInfo"></param>
        /// <param name="userStateType"></param>
        /// <param name="Expires"></param>
        /// <param name="extValues">扩展值</param>
        /// <param name="context"></param>
        /// <returns></returns>
        TokenInfo SetUserLoginState(BaseUser userInfo, LoginUserStateType userStateType, DateTime Expires, LoginPlatfromId platForm, object extValues, DBContext context);



        /// <summary>
        /// 从用户请求中获取信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        BaseUser GetUserFrom(DBContext context);

        string GetRequestHeader(DBContext context, string name);

        void OnDBContextInit(DBContext context);

        /// <summary>
        /// 当从地址栏获取权限名
        /// </summary>
        string OnUrlPermission(string url);


        /// <summary>
        /// 从appsetting配置文件取值
        /// </summary>
        T GetSettingValue<T>(DBContext context, string path, bool throwNoFoundError = false, T defaultVal = default(T));

        /// <summary>
        /// 日志记录
        /// </summary>       
        void LogMessage(string message, string moduleName, DBContext context);

        /// <summary>
        /// 异常日志记录
        /// </summary>  
        void LogException(Exception ex, string moduleName, DBContext context);

        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="context"></param>
        void LogDebug(string message,string moduleName, DBContext context);
        

        /// <summary>
        /// 清除缓存，重新加载配置
        /// </summary>  
        void ClearCache(DBContext context);

        /// <summary>
        /// Permission文件的内容
        /// </summary>  
        Dictionary<string, QueryEntityConfig> GetPermissionQueryMapping(DBContext context);
        
        /// <summary>
        /// 获取登录token信息，没登录返回null
        /// </summary>
        TokenInfo GetTokenInfo(DBContext context);

        /// <summary>
        /// 自定义生成自增标识
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="primaryKey">主键</param>
        /// <param name="dBContext"></param>
        /// <returns></returns>
        object GetCustomIdentityValue(string tableName, string primaryKey, DBContext context);
        
        /// <summary>
        /// 创建业务层（服务层）实例
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        IBusiness CreateServiceInstance(Type type,DBContext context);

        
        /// <summary>
        /// Context关闭（连接，事务）
        /// </summary>
        /// <param name="dBContext"></param>
        void OnDBContextClosed(DBContext context);

        /// <summary>
        /// context释放
        /// </summary>
        /// <param name="context"></param>
        void OnDBContextDisposed(DBContext context);

        string GetNacosUrl(string nacosServiceName, DBContext mainContext);

    }
}
